﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using System.Data;
using Telerik.Web.UI;
using KPIS.Utils;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class RC_Commands : System.Web.UI.UserControl
    {
        private object _dataItem = null;
        private SsMng ssMng = new SsMng();
        private ICommandBLL cmdBLL = new CommandBLL();
        private ICommandTypeBLL ctBLL = new CommandTypeBLL();
        private IMasRecordStatusBLL staComBLL = new MasRecordStatusBLL();
        private IEmployeeTypeBLL empTypeBLL = new EmployeeTypeBLL();
        private IEmployeeBLL empBLL = new EmployeeBLL();
        private IPositionBLL posBLL = new PositionBLL();
        private IJobSiteLevelSystemControlBLL jslscBLL = new JobSiteLevelSystemControlBLL();
        private IJobSiteTypeBLL jobTypeBLL = new JobSiteTypeBLL();

        //ReadOnly property.
        protected void Page_Load(object sender, EventArgs e)
        {

        }   
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }
        //Initializing the new variable updating ViewState data.
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);            

            if (!IsUserControlPostBack)
            {
                this.ViewState.Add("IsUserControlPostBack", true);

                lblCmdType.Text = Session["CMDTYPE"].ToString();

                bindStaCom();
                FillDataToApprover();

                DataSet ds = new DataSet();
                ds = jslscBLL.ListJobSiteLevelSystemControl();
                bool bMis = ds.Tables[0].Rows[0][0].ToString() == "1";
 
                rdoFlag.Items[0].Enabled = !bMis;
                rdoFlag.Items[1].Selected = bMis;
                rdoApptypeOut.Enabled = !bMis;
                txtAppName.Enabled = !bMis;
                rdoFlag.Items[0].Selected = !bMis;               
                
            }

            if (DataItem != null)
            {                

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblID.Text = DataBinder.Eval(DataItem, "cmd_seq").ToString();
                    ddlStatus.SelectedValue = DataBinder.Eval(DataItem, "cmd_status").ToString();
                    string strFlag = DataBinder.Eval(DataItem, "cmd_flag").ToString();
                    if (strFlag == "1")
                        rdoFlag.Items[0].Selected = true;
                    if (strFlag == "2")
                        rdoFlag.Items[1].Selected = true;
                    if (strFlag == "3")
                        rdoFlag.Items[2].Selected = true;

                    txtNo.Text = DataBinder.Eval(DataItem, "cmd_draft_no").ToString();
                    txtRefNo.Text = DataBinder.Eval(DataItem, "cmd_ref_no").ToString();
                    txtDescr.Text = DataBinder.Eval(DataItem, "cmd_descr").ToString();
                    txtReportHeader.Text = DataBinder.Eval(DataItem, "rpt_header").ToString();

                    lblCmdType.Text = DataBinder.Eval(DataItem, "cmd_type_seq").ToString();
                    txtDateApproved.SetValue(DataBinder.Eval(DataItem, "approved_date", "{0:dd/MM/yyyy}").ToString());

                    rdoApptypeIn.Checked = DataBinder.Eval(DataItem, "approved_seq") != null;
                    rdoApptypeOut.Checked = !rdoApptypeIn.Checked;

                    txtAppName.Text = DataBinder.Eval(DataItem, "approved_name").ToString();

                    if (DataBinder.Eval(DataItem, "approved_seq") != null)
                    {
                        cmbEmployee.SelectedValue = DataBinder.Eval(DataItem, "approved_seq").ToString();
                        txtAppName.Text = "";
                    }

                    cmbEmployee.Enabled = rdoApptypeIn.Checked;
                    txtAppName.Enabled = rdoApptypeOut.Checked;
                }
                else
                {
                    int nCmdType = lblCmdType.Text.Length > 0 ? Int32.Parse(lblCmdType.Text) : 0;

                    DataSet ds = ctBLL.LoadCommandTypes(nCmdType, (int?)null);
                    DataTable dt = null;
                    if (ds.Tables.Count > 0)
                    {
                        dt = ds.Tables[0];
                        if (dt.Rows.Count > 0)
                        {
                            txtReportHeader.Text = dt.Rows[0]["RPT_HEADER"].ToString();
                            txtDescr.Text = dt.Rows[0]["RPT_FORMAT"].ToString();
                        }
                        else 
                        {
                            txtReportHeader.Text = "";
                            txtDescr.Text = "";
                        }
                    }
                    txtNo.Text = cmdBLL.GetNextDraftNo(nCmdType.ToString());
                }
            }
        }     

        private void bindStaCom()
        {
            IList<MasRecordStatusInfo> ListStatusCommand = staComBLL.ListStatusCommand();
            ddlStatus.DataSource = ListStatusCommand;
            ddlStatus.DataBind();
        }

        public void FillDataToApprover()
        {
            DataSet ds = empBLL.LoadEmployeeForApproveCommands();
            DataTable dt = ds.Tables[0];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                RadComboBoxItem item = new RadComboBoxItem();

                item.Text = dt.Rows[i]["EMP_NAME"].ToString();
                item.Value = dt.Rows[i]["EMP_SEQ"].ToString();

                item.Attributes.Add("EMP_NAME", dt.Rows[i]["EMP_NAME"].ToString());
                item.Attributes.Add("EXEC_POS_NAME", dt.Rows[i]["EXEC_POS_NAME"].ToString());

                cmbEmployee.Items.Add(item);
                item.DataBind();
            }
        }
        public void CheckValidate()
        {
            reqDraftNo.Enabled = ddlStatus.SelectedValue == SystemInfo._WorkRecordStatus;
            reqRefNo.Enabled = ddlStatus.SelectedValue == SystemInfo._ApproveRecordStatus;
            reqEmployee.Enabled = rdoApptypeIn.Checked;
            reqAppName.Enabled = rdoApptypeOut.Checked;
        }
        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            try
            {
                CheckValidate();

                cmdBLL.InsertCommandEmployee(SystemInfo._CmdEmployee
                    , rdoFlag.SelectedValue
                    , txtNo.Text.Trim()
                    , txtRefNo.Text
                    , txtName.Text.Trim()
                    , txtDescr.Text.Trim()
                    , txtReportHeader.Text.Trim()
                    , ddlStatus.SelectedValue
                    , Int32.Parse(Session["EMPTYPE"].ToString())
                    , Int32.Parse(lblCmdType.Text)
                    , cmbEmployee.SelectedValue.Length > 0 ? int.Parse(cmbEmployee.SelectedValue) : (int?)null
                    , txtAppName.Text.Trim()
                    , txtAppPosition.Text.Trim()
                    , txtDateApproved.GetStringValue().Length > 0 ? DateTime.Parse(txtDateApproved.GetStringValue()) : (DateTime?)null
                    , txtRemarks.Text.Trim()
                    , ssMng.EmpSeq == null ? 0 : ssMng.EmpSeqI);
            }
            catch (Exception ex)
            {

            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                CheckValidate();

                cmdBLL.UpdateCommandEmployee(SystemInfo._CmdEmployee
                   , Convert.ToInt32(lblID.Text.Trim())
                   , rdoFlag.SelectedValue
                   , txtNo.Text.Trim()
                   , txtRefNo.Text
                   , txtName.Text.Trim()
                   , txtDescr.Text.Trim()
                   , txtReportHeader.Text.Trim()
                   , ddlStatus.SelectedValue
                   , Int32.Parse(Session["EMPTYPE"].ToString())
                   , Int32.Parse(lblCmdType.Text)
                   , cmbEmployee.SelectedValue.Length > 0 ? int.Parse(cmbEmployee.SelectedValue) : (int?)null
                   , txtAppName.Text.Trim()
                   , txtAppPosition.Text.Trim()
                   , txtDateApproved.GetStringValue().Length > 0 ? DateTime.Parse(txtDateApproved.GetStringValue()) : (DateTime?)null
                   , txtRemarks.Text.Trim()
                   , ssMng.EmpSeqI);
            }
            catch (Exception ex)
            {

            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            txtAppName.Enabled = false;            
            cmbEmployee.Enabled = true;
            cmbEmployee.ClearSelection();
            cmbEmployee.Text = string.Empty;
            txtAppName.Text = "";
        }
        protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
        {
            txtAppName.Enabled = true;
            cmbEmployee.Enabled = false;

            cmbEmployee.ClearSelection();
            cmbEmployee.Text = string.Empty;
        }
        protected void cmbEmployee_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            Nullable<int> nEmpSeq = string.IsNullOrEmpty(cmbEmployee.SelectedValue) ? (int?)null : Int32.Parse(cmbEmployee.SelectedValue);
            DataSet ds = empBLL.LoadEmployees(nEmpSeq, "", true, "");
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                txtAppPosition.Text = dt.Rows[0]["EXEC_POS_NAME"].ToString();
            }

        }

        protected void ddlStatus_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {

        }

    }
}